//
//  MyCertifyViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/18.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "MyCertifyViewController.h"

@interface MyCertifyViewController () <UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (strong, nonatomic) UITableView * tableview;
@property (strong, nonatomic) UIButton * btn;
@property (nonatomic,assign) BOOL isSelect;
@property (strong, nonatomic) UIImage * selectImg;
@property (strong, nonatomic) UITapGestureRecognizer * tap;

@end

@implementation MyCertifyViewController

static NSString * cellID = @"cell";
static NSString * PropertyReparisInputInfoTableViewCellID = @"PropertyReparisInputInfoTableViewCell";
static NSString * MyCertifyImgTableViewCellID = @"MyCertifyImgTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的认证";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.btn];
    [self creatTableview];
    [self registCell];
}

-(UIButton *)btn
{
    if (_btn == nil) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
        [_btn setTitle:@"修改" forState:UIControlStateNormal];
        [_btn setTitle:@"确认" forState:UIControlStateSelected];
        _btn.titleLabel.font = font(16);
        [_btn setTitleColor:SFM51 forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}

-(void)rightClick
{
    self.isSelect = self.btn.selected = !self.btn.selected;
    [self.tableview reloadData];
}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[PropertyReparisInputInfoTableViewCell class] forCellReuseIdentifier:PropertyReparisInputInfoTableViewCellID];
    [self.tableview registerClass:[MyCertifyImgTableViewCell class] forCellReuseIdentifier:MyCertifyImgTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.backgroundColor = SFM242;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 40;
    }
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
        line.backgroundColor = SFM242;
        
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, WIDTH - 40, 20)];
        titleLabel.font = font(14);
        titleLabel.textColor = SFM153;
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.text = @"营业执照照片";
        [line addSubview:titleLabel];
        
        return line;
    }
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 55;
    }
    return 170;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 6;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PropertyReparisInputInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PropertyReparisInputInfoTableViewCellID];
        if (self.isSelect) {
            cell.textfiled.enabled = YES;
        }else
        {
            cell.textfiled.enabled = NO;
        }
        if (indexPath.row == 0) {
            cell.titleLabel.text = @"名称";
            cell.textfiled.placeholder = @"这是一个公司名称";
        }else if(indexPath.row == 1)
        {
            cell.titleLabel.text = @"经营者姓名";
            cell.textfiled.placeholder = @"张三";
        }else if(indexPath.row == 2)
        {
            cell.titleLabel.text = @"组成形式";
            cell.textfiled.placeholder = @"个人经营";
        }else if(indexPath.row == 3)
        {
            cell.titleLabel.text = @"经营场所";
            cell.textfiled.placeholder = @"这个是场所名称";
        }else if(indexPath.row == 4)
        {
            cell.titleLabel.text = @"经营范围";
            cell.textfiled.placeholder = @"加工、纺织";
        }else
        {
            cell.titleLabel.text = @"有效日期至";
            cell.textfiled.placeholder = @"2018年08月09日";
        }
        return cell;
    }else
    {
        MyCertifyImgTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyCertifyImgTableViewCellID];
        if (self.selectImg != nil) {
            cell.img.image = self.selectImg;
        }else
        {
            cell.img.backgroundColor = SFMRandColor;
        }

        if (self.isSelect) {
            [cell.img addGestureRecognizer:self.tap];
        }else
        {
            [cell.img removeGestureRecognizer:self.tap];
        }
        return cell;
    }
}

-(UITapGestureRecognizer *)tap
{
    if (_tap == nil) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgTap)];
    }
    return _tap;
}

-(void)imgTap
{
    if (self.isSelect) {
        [self chooseMedthod];
    }
}

-(void)chooseMedthod
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"选择" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    WeakSelf;
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = weakSelf;
        [strongSelf presentViewController:picker animated:YES completion:nil];
    }];
    
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf takePhoto];
    }];
    
    UIAlertAction * action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction: action1];
    [alert addAction: action2];
    [alert addAction: action3];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma  mark -开始拍照
-(void)takePhoto
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        //        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

#pragma mark -当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        self.selectImg = image;
        [self.tableview reloadData];
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
